'use strict'


const Controller = require('egg').Controller

class MainController extends Controller {
    async index() {
        this.ctx.body = 'hi admin api'
    }

    async checkLogin() {
        // this.ctx.res.setHeader("Access-Control-Allow-Credentials", "true");
        let userName = this.ctx.request.body.userName
        let passWord = this.ctx.request.body.passWord
        const sql = /*sql*/`SELECT * FROM admin_user where userName = '${userName}' AND passWord = '${passWord}'`
        const res = await this.app.mysql.query(sql)

        // console.log("login---res", res);
        if (res.length > 0) {
            let openId = new Date().getTime()
            this.ctx.session.openId = { 'openId': openId }
            // console.log("this.ctx===checkLogin",this.ctx.session)
            this.ctx.body = { data: "登录成功", "openId": openId, code: "0000" }
        } else {
            this.ctx.body = { data: "登录失败", code: "9999" }
        }
    }

    async getTypeInfo() {
        const res = await this.app.mysql.select("type")
        this.ctx.body = { data: res, code: "0000" };
    }

    // 添加文章
    async addArticle() {
        let tmpArticle = this.ctx.request.body

        const res = await this.app.mysql.insert('article', tmpArticle)
        const insertSuccess = res.affectedRows === 1
        const insertId = res.insertId

        this.ctx.body = {
            code: "0000",
            isSuccess: insertSuccess,
            insertId: insertId
        }
    }

    // 更新文章
    async updateArticle() {
        let tmpArticle = this.ctx.request.body

        const res = await this.app.mysql.update('article', tmpArticle)
        const updateSuccess = res.affectedRows === 1

        console.log("updateSuccess ==updateSuccess", res, updateSuccess)

        this.ctx.body = {
            code: "0000",
            isSuccess: updateSuccess,
        }
    }

    //获得文章列表
    async getArticleList() {

        let sql = /*sql*/`SELECT article.id as id,
            article.title as title,
            article.introduce as introduce,
            article.view_count  as view_count,
            FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime,
            type.typeName as typeName 
            FROM article LEFT JOIN type ON article.type_id = type.Id 
            ORDER BY article.id DESC`

        const resList = await this.app.mysql.query(sql)
        this.ctx.body = { data: resList, code: "0000" }

    }

    // 删除文章
    async delArticle() {

        let id = this.ctx.params.id
        const res = await this.app.mysql.delete("article", { "id": id })

        this.ctx.body = { data: res }

    }

    //根据文章ID得到文章详情，用于修改文章
    async getArticleById() {
        let id = this.ctx.params.id

        let sql = /*sql*/`SELECT article.id as id,
            article.title as title,
            article.introduce as introduce,
            article.article_content as article_content,
            FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime,
            article.view_count as view_count ,
            type.typeName as typeName ,
            type.id as typeId 
            FROM article LEFT JOIN type ON article.type_id = type.Id
            WHERE article.id=${id}`
        const res = await this.app.mysql.query(sql)
        this.ctx.body = { data: res }
    }
}


module.exports = MainController